package com.xcl.service.impl;

import com.xcl.mapper.LoadingMapper;
import com.xcl.mapper.XclWaybillMapper;
import com.xcl.pojo.Loading;
import com.xcl.pojo.Vehicle;
import com.xcl.service.LoadingServiceInter;
import com.zzg.entity.Driver;
import com.zzg.entity.TmsRegion;
import com.zzg.entity.Transportprotocol;
import com.zzg.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @ClassName LoadingServiceImpl
 * @Description 装车配载服务层实现类
 * @Author: xiaochaolei
 * @date 2021/6/28 11:10
 * @Version 1. 0
 */
@Service
public class LoadingServiceImpl implements LoadingServiceInter {
    @Autowired
    LoadingMapper loadingMapper;

    @Autowired
    XclWaybillMapper waybillMapper;

    @Override
    public List<Loading> getLoadingBYStateOne() {
        return loadingMapper.getLoadingBYStateOne();
    }

    @Override
    public List<Loading> getLoadingBYStateOneLimit(int page, int limit) {
        return loadingMapper.getLoadingBYStateOneLimit((page-1)*limit,limit);
    }

    @Override
    public List<Loading> getLoadingBYStateTwo() {
        return loadingMapper.getLoadingBYStateTwo();
    }

    @Override
    public List<Loading> getLoadingBYStateTwoLimit(int page, int limit) {
        return loadingMapper.getLoadingBYStateTwoLimit((page-1)*limit,limit);
    }

    @Override
    public List<Loading> getLoadingBYStateThree() {
        return loadingMapper.getLoadingBYStateThree();
    }

    @Override
    public List<Loading> getLoadingBYStateThreeLimit(int page, int limit) {
        return loadingMapper.getLoadingBYStateThreeLimit((page-1)*limit,limit);
    }
    @Override
    public int insertTransportprotocol(Transportprotocol transportprotocol) {
        return loadingMapper.insertTransportprotocol(transportprotocol);
    }

    @Override
    public int insertDriver(Driver driver,Transportprotocol transportprotocol) {
        //用于储存新增地区的id
        String one="";
        String tow="";
        String three="";
        //得到路线一
        String trafficcircuitone = driver.getTrafficcircuitone();
        //得到路线二
        String trafficcircuittwo = driver.getTrafficcircuittwo();
        //得到车辆位置
        String location = driver.getLocation();
        // 路线一判断是否存在
        TmsRegion existConsignerArea = isExistConsignerArea(trafficcircuitone);
        //为空就新增
        if (existConsignerArea==null){
            //根据-分割
            String[] split = trafficcircuitone.split("-");
            //new一个地区对象
            TmsRegion tmsRegion=new TmsRegion();
            //设置id
            tmsRegion.setRegionId(UuidUtil.getUuid());
            //得到id
            one=tmsRegion.getRegionId();
            //设置省
            tmsRegion.setProvince(split[0]);
            //设置市
            tmsRegion.setCity(split[1]);
            //设置区
            tmsRegion.setDistrict(split[2]);
            //设置详细地址
            tmsRegion.setDetailedAddress(trafficcircuitone);
            //新增地区
            waybillMapper.insertTmsRegion(tmsRegion);
        }else {
            //已存在就得到id
            one=existConsignerArea.getRegionId();
        }

        // 路线二判断是否存在
        TmsRegion existConsignerArea2 = isExistConsignerArea(trafficcircuittwo);
        //为空就新增
        if (existConsignerArea2==null){
            //根据-分割
            String[] split = trafficcircuittwo.split("-");
            //new一个地区对象
            TmsRegion tmsRegion=new TmsRegion();
            //设置id
            tmsRegion.setRegionId(UuidUtil.getUuid());
            //得到id
            tow=tmsRegion.getRegionId();
            //设置省
            tmsRegion.setProvince(split[0]);
            //设置市
            tmsRegion.setCity(split[1]);
            //设置区
            tmsRegion.setDistrict(split[2]);
            //设置详细地址
            tmsRegion.setDetailedAddress(trafficcircuittwo);
            //新增地区
            waybillMapper.insertTmsRegion(tmsRegion);
        }else {
            //已存在就得到id
            tow=existConsignerArea2.getRegionId();
        }
        // 车辆位置判断是否存在
        TmsRegion existConsignerArea3= isExistConsignerArea(location);
        //为空就新增
        if (existConsignerArea3==null){
            //根据-分割
            String[] split = location.split("-");
            //new一个地区对象
            TmsRegion tmsRegion=new TmsRegion();
            //设置id
            tmsRegion.setRegionId(UuidUtil.getUuid());
            //得到id
            three=tmsRegion.getRegionId();
            //新增地区
            //设置省
            tmsRegion.setProvince(split[0]);
            //设置市
            tmsRegion.setCity(split[1]);
            //设置区
            tmsRegion.setDistrict(split[2]);
            //设置详细地址
            tmsRegion.setDetailedAddress(location);
            //新增地区
            waybillMapper.insertTmsRegion(tmsRegion);
        }else {
            //已存在就得到id
            three=existConsignerArea3.getRegionId();
        }
        //设置路线一二以及车辆信息
        driver.setLocation(three);
        driver.setTrafficcircuitone(one);
        driver.setTrafficcircuittwo(tow);
        //设置协议信息id
        transportprotocol.setTransportprotocolId(UuidUtil.getUuid());
        //得到当前毫秒数
        Calendar calendar = Calendar.getInstance();
        long timeInMillis = calendar.getTimeInMillis();
        //设置到运输单号中
        transportprotocol.setTrackingnumber(timeInMillis);
        //设置协议信息中的车辆id
        transportprotocol.setDriverId(driver.getDriverId());
        //新增
        loadingMapper.insertDriver(driver);
        //新增
        loadingMapper.insertTransportprotocol(transportprotocol);
        return 1;
    }

    @Override
    public int insertTmsRegion(TmsRegion tmsRegion) {
        return waybillMapper.insertTmsRegion(tmsRegion);
    }

    @Override
    public TmsRegion isExistConsignerArea(String area) {
        return waybillMapper.isExistConsignerArea(area);
    }

    @Override
    public List<Loading> getLoadingByWhere(String waybill_id, String substationUnit, String modeTransport, String startDate, String endingDate, int page, int limit) {
        return loadingMapper.getLoadingByWhere(waybill_id,substationUnit,modeTransport,startDate,endingDate,(page-1)*limit,limit);
    }

    @Override
    public List<Loading> getLoadingByWhereTwo(String waybill_id, String modeTransport, int page, int limit) {
        return loadingMapper.getLoadingByWhereTwo(waybill_id,modeTransport,(page-1)*limit,limit);
    }

    @Override
    public int upteModificationOne(String[] strings) {
        return loadingMapper.upteModificationOne(strings);
    }

    @Override
    public int upteModificationTwo(String[] strings) {
        return loadingMapper.upteModificationTwo(strings);
    }

    @Override
    public int upteModificationThree(String[] strings) {
        return loadingMapper.upteModificationThree(strings);
    }

    @Override
    public int upteModification(String id) {
        return loadingMapper.upteModification(id);
    }

    @Override
    public List<String> getOwnerNameByDriver() {
        List<String> list=new ArrayList<>();
        List<Driver> allConsigneeByClientname =loadingMapper.getOwnerNameByDriver();
        for (int i = 0; i < allConsigneeByClientname.size(); i++) {
            list.add(allConsigneeByClientname.get(i).getOwnername()+"（编号"+ allConsigneeByClientname.get(i).getDriverId()+")");
        }
        return list;
    }

    @Override
    public Vehicle getDriverByDriverId(String id) {
        Vehicle driverByDriverId = loadingMapper.getDriverByDriverId(id);
        return  driverByDriverId;
    }


}
